/*
 * Copyright (C) 2018 The Asiainfo-Ability Authors
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package org.asiainfo.ability.crm.query;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.*;
import org.asiainfo.ability.base.code.SystemCode;
import org.asiainfo.ability.crm.query.service.QueryUserService;
import org.asiainfo.ability.crm.query.vo.SearchCondition;
import org.asiainfo.ability.crm.query.vo.UserResult;
import org.asiainfo.ability.provider.annotation.AbilityFace;
import org.asiainfo.ability.provider.annotation.ApiProvider;
import org.asiainfo.ability.provider.custom.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: visen
 * @Date: 2018/1/25
 * @Description:
 */

@AbilityFace
@Api(tags = {"123"}, hidden = true)
public class QueryUser {


    @Value("${server.port}")
    private String port;

    @Autowired
    private RestTemplate restTemplate;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "clientId", dataType = "String", required = true, value = "接入标识"),
            @ApiImplicitParam(paramType = "header", name = "token", dataType = "String", required = true, value = "token串"),
            @ApiImplicitParam(paramType = "query", name = "serialNumber", dataType = "String", required = true, value = "token串"),
            @ApiImplicitParam(paramType = "query", name = "tradeTypeCode", dataType = "String", required = false, value = "token串"),
            @ApiImplicitParam(paramType = "query", name = "netTypeCode", dataType = "String", required = false, value = "token串"),
            @ApiImplicitParam(paramType = "query", name = "tag", dataType = "String", required = false, value = "token串")
    })
    @ApiOperation(value = "用户资料查询", notes = "用户资料查询", produces = "application/json")
    @ApiResponses(
            {@ApiResponse(code = 400, message = "平台级错误，参考码表", response = SystemCode.class)
                    , @ApiResponse(code = 200, message = "平台级错误，参考码表", response = UserResult.class)}
    )
    @RequestMapping(value = "/user-query", method = RequestMethod.GET)
    //@HystrixCommand(fallbackMethod = "fallback")
    @ApiProvider
    public @ResponseBody
    Object process(SearchCondition searchCondition, HttpServletRequest request
            , HttpServletResponse response) throws Exception {
        UserResult result = new UserResult();
        result.setSerialNumber(searchCondition.getSerialNumber());
        result.setUserId("1234567678682342442");
        result.setCustName("李四===" + port);
        result.setProductId("121333");
        if (true) {
            //throw new org.asiainfo.ability.base.exception.SystemException("aa");
        }
        Map param = new HashMap();
        //UserResult u = restTemplate.getForObject("http://crm-userquery-service/api/v1/user/query2", UserResult.class, param);
        return result;
    }

    public Object fallback(SearchCondition searchCondition, HttpServletRequest request
            , HttpServletResponse response) {
        return "3333";
    }


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "clientId", dataType = "String", required = true, value = "接入标识"),
            @ApiImplicitParam(paramType = "header", name = "token", dataType = "String", required = true, value = "token串"),
            @ApiImplicitParam(paramType = "query", name = "serialNumber", dataType = "String", required = true, value = "token串"),
            @ApiImplicitParam(paramType = "query", name = "tradeTypeCode", dataType = "String", required = false, value = "token串"),
            @ApiImplicitParam(paramType = "query", name = "netTypeCode", dataType = "String", required = false, value = "token串"),
            @ApiImplicitParam(paramType = "query", name = "tag", dataType = "String", required = false, value = "token串")
    })
    @ApiOperation(value = "用户资料查询", notes = "用户资料查询", produces = "application/json")
    @ApiResponses(
            {@ApiResponse(code = 400, message = "平台级错误，参考码表", response = SystemCode.class)
                    , @ApiResponse(code = 200, message = "平台级错误，参考码表", response = UserResult.class)}
    )
    @RequestMapping(value = "/user/query2", method = RequestMethod.GET, produces = "application/xml")
    @ApiProvider
    public @ResponseBody
    Object queryUser2(SearchCondition searchCondition, HttpServletRequest request
            , HttpServletResponse response) throws Exception {
        UserResult result = new UserResult();
        result.setSerialNumber(searchCondition.getSerialNumber());
        result.setUserId("1234567678682342442");
        result.setCustName("李四===" + port);
        result.setProductId("121333");
        if (true) {
            //throw new org.asiainfo.ability.base.exception.SystemException("aa");
        }
        return result;
    }

    public static void main(String[] args) throws  Exception {
        ObjectMapper m = new ObjectMapper();
        byte[] data = "{\"tag\":\"111\",\"serialNumber\":\"111\",\"tag3\":\"111\",\"tag4\":\"111\"}".getBytes();
        ByteArrayInputStream io = new ByteArrayInputStream(data);
        long old = System.currentTimeMillis();

        for(int i = 0; i < 1000000; i++) {
            //io = new ByteArrayInputStream(data);
            m.readValue("{\"tag\":\"111\",\"serialNumber\":\"111\",\"tag3\":\"111\",\"tag4\":\"111\"}"
                    , SearchCondition.class);
        }
        System.out.println(System.currentTimeMillis() - old);
    }

}
